<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/5/15
 * Time: 11:41
 */

namespace app\home\controller;


use think\Controller;
use think\Db;
use think\Request;

class UserController extends Controller
{
	//手机端的浏览量
	public function wapNum(){
        header("Access-Control-Allow-Origin:*");
		Db::table('ew_number')->where('id',1)->setInc('num');
		$ip = $this->getip();
		$data = array();
		$data['ip'] = $ip;
		$data['time'] = date('Y-m-d H:i:s');
		$data['type'] = $_POST['type'];
		if(isset($_POST['from'])){
            $data['from'] = $_POST['from'];
        }
        Db::table('ew_ip')->insert($data);
	}
	//end
    public function userinfowap(){
		header("Access-Control-Allow-Origin:*");
		//return json_encode(['msg' => '注册失败', 'status' => 0]);
            $username = trim(input('username'));
            $phone = trim(input('phone'));
            if($username == ''){
                return json_encode(['msg' => '用户名不能为空', 'status' => 0]);
            }else{
                if($phone == ''){
                    return json_encode(['msg' => '手机号码不能为空', 'status' => 0]);
                }else{
                    if(!preg_match("/^[1][3578][0-9]{9}$/",$phone)){
                        return json_encode(['msg' => '请填写正确的11位手机号', 'status' => 0]);
                    }else{
                        $res = Db::table('ew_user')->where('phone', $phone)->find();
                        if($res){
                            return json_encode(['msg' => '该手机号码已注册', 'status' => 0]);
                        }else{
                            $params['name'] = $username;
                            $params['phone'] = $phone;
                            $params['inserttime'] = date('Y-m-d H:i:s', time());
                            $params['number'] = $this->getDraw();
                            $params['wap'] = 1;//手机网站来的
                            $row = Db::table('ew_user')->insert($params);
                            if($row){
                                return json_encode(['msg' => '报名成功', 'status' => 1, 'number' => $params['number']]);
                            }else{
                                return json_encode(['msg' => '注册失败', 'status' => 0]);
                            }
                        }
                    }
                }
            }
    }
    function getip() {
        $unknown = 'unknown';
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        /** * 处理多层代理的情况
         * 或者使用正则方式：$ip = preg_match("/[\d.]{7,15}/", $ip, $matches) ? $matches[] : $unknown;
         */
        if (false !== strpos($ip, ',')) $ip = reset(explode(',', $ip));
        return $ip;
    }
/*-------------------------------------------------------------------------------------------------*/
    public function userinfo(){
        if(Request::instance()->isPost()){
            $username = trim(input('username'));
            $phone = trim(input('phone'));
            if($username == ''){
                return json_encode(['msg' => '用户名不能为空', 'status' => 0]);
            }else{
                if($phone == ''){
                    return json_encode(['msg' => '手机号码不能为空', 'status' => 0]);
                }else{
                    if(!preg_match("/^[1][3578][0-9]{9}$/",$phone)){
                        return json_encode(['msg' => '请填写正确的11位手机号', 'status' => 0]);
                    }else{
                        $res = Db::table('ew_user')->where('phone', $phone)->find();
                        if($res){
                            return json_encode(['msg' => '该手机号码已注册', 'status' => 0]);
                        }else{
                            $params['name'] = $username;
                            $params['phone'] = $phone;
                            $params['inserttime'] = date('Y-m-d H:i:s', time());
                            $params['number'] = $this->getDraw();
                            $row = Db::table('ew_user')->insert($params);
                            if($row){
                                return json_encode(['msg' => '报名成功', 'status' => 1, 'number' => $params['number']]);
                            }else{
                                return json_encode(['msg' => '注册失败', 'status' => 0]);
                            }
                        }
                    }
                }
            }
        }else{
            $img_url = Db::table('ew_userbg')->where('id', 7)->value('bgpic');
            $this->assign('img_url', $img_url);
            return $this->fetch();
        }
    }
    
    /**
     * 生成中奖码
     */
    public function getDraw(){
        $letter ='';
        for($i=1; $i<=2; $i++){
            $letter .= chr(rand(65, 90));
        }
        $num = rand(100000, 999999);
        $number = $letter . $num;
        $result = Db::table('ew_user')->where('number', $number)->find();
        if($result){
            $this->getDraw();
        }
        return $number;
    }
    public function drawcode($number){
        $bg_img = Db::table('ew_last')->where('id', 1)->value('pic');
        $this->assign('bg_img', $bg_img);
        $this->assign('number', $number);
        return $this->fetch();
    }
}